package com.imti.mapper;

import com.imti.model.Clazz;
import com.imti.model.dto.ClazzUser;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface ClazzMapper {

    @Results(id = "findAllClazz",value = {
            @Result(column = "user_id",property = "userId"),
            @Result(column = "create_time",property = "createTime")
    })
    @Select("select * from t_clazz")
    public List<Clazz> findAllClazz(Clazz clazz);

    @ResultMap("findAllClazz")
    @Insert("insert into t_clazz (name,user_id,create_time) values(#{name},#{userId},CURRENT_TIMESTAMP())")
    public int addClazzById(Clazz clazz);

    @Delete("delete from t_clazz where id = #{id}")
    int deleteClazz(Integer id);

    @ResultMap("findAllClazz")
    @Select("SELECT tc.`name`,u.* FROM t_user u,user_clazz uc,t_clazz tc\n" +
            "WHERE tc.`id`=#{id} and uc.`user_id`=u.`id`;")
    List<ClazzUser> select(ClazzUser clazz);

    @Results({
            @Result(column = "id",property = "userId"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "name",property = "clazzName")
    })
    @Select("SELECT tc.`name`,u.`id`,u.`username`,u.`phone`,u.`create_time` FROM t_user u,user_clazz uc,t_clazz tc\n" +
            "WHERE tc.`id`=#{id} and uc.`user_id`=u.`id`and role ='1';\n")
    List<ClazzUser> selectClazz(Integer id);
}
